/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.persistence.hibernate.core;

import com.blandware.atleap.common.util.PartialCollection;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.core.ContentImage;
import com.blandware.atleap.model.core.ResourceData;
import com.blandware.atleap.model.core.Role;
import com.blandware.atleap.persistence.core.ContentImageDAO;
import com.blandware.atleap.persistence.exception.DeleteException;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
 * <p>Hibernate implementation of ContentImageDAO</p>
 * <p><a href="ContentImageDAOHibernate.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.11 $ $Date: 2008/06/23 13:24:53 $
 */
public class ContentImageDAOHibernate extends ContentResourceDAOHibernate implements ContentImageDAO {

	/**
	 * Creates new instance of ContentImageDAOHibernate
	 */
	public ContentImageDAOHibernate() {
	}

	// ~ CRUD Methods ================================================================

	/**
	 * @see com.blandware.atleap.persistence.core.ContentImageDAO#createContentImage(com.blandware.atleap.model.core.ContentImage, com.blandware.atleap.model.core.ResourceData)
	 */
	public Long createContentImage(ContentImage contentImage, ResourceData resourceData) {
        contentImage.setLastUpdatedDatetime(new Date());
		contentImage.setUsageCounter(new Integer(0));
		getHibernateTemplate().save(resourceData);
		contentImage.setResourceData(resourceData);
		return (Long) getHibernateTemplate().save(contentImage);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentImageDAO#retrieveContentImage(Long)
	 */
	public ContentImage retrieveContentImage(Long contentImageId) {
		return (ContentImage) getHibernateTemplate().get(ContentImage.class, contentImageId);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentImageDAO#updateContentImage(com.blandware.atleap.model.core.ContentImage, com.blandware.atleap.model.core.ResourceData)
	 */
	public void updateContentImage(ContentImage contentImage, ResourceData resourceData) {
        contentImage.setLastUpdatedDatetime(new Date());
		getHibernateTemplate().saveOrUpdate(resourceData);
		contentImage.setResourceData(resourceData);
		getHibernateTemplate().update(contentImage);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentImageDAO#deleteContentImage(com.blandware.atleap.model.core.ContentImage)
	 */
	public void deleteContentImage(ContentImage contentImage) throws DeleteException {
		getHibernateTemplate().delete(contentImage);

		// break relations between roles and deleted image
		List roles = new ArrayList(contentImage.getRoles());
		for ( int i = 0; i < roles.size(); i++ ) {
			Role role = (Role) roles.get(i);
			contentImage.removeRole(role);
		}
	}

	// ~ Additional methods ================================================================

	/**
	 * @see com.blandware.atleap.persistence.core.ContentImageDAO#listContentImages(com.blandware.atleap.common.util.QueryInfo)
	 */
	public PartialCollection listContentImages(QueryInfo queryInfo) {
		String whereClause = new String();
		String orderByClause = new String();
		if ( queryInfo != null ) {
			whereClause = queryInfo.getWhereClause();
			orderByClause = queryInfo.getOrderByClause();
			if ( whereClause != null ) {
				whereClause = " where " + whereClause;
			} else {
				whereClause = new String();
			}
			if ( orderByClause != null && orderByClause.length() != 0 ) {
				orderByClause = " order by " + orderByClause;
			} else {
				orderByClause = new String();
			}
		}

		List list = null;
		Integer total = null;
		String hqlPart = null;
		hqlPart = "from ContentImage r left outer join r.roles as role";

		hqlPart += whereClause;

		if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
			String hqlForTotal = "select count(distinct r.id) " + hqlPart;
			total = findUniqueIntegerResult(hqlForTotal);
			if ( total == null ) {
				total = new Integer(0);
			}
		}

        // If we don't have any info about the total number of results yet or
        // we know that there's something that will be found, then fetch data
		if ( total == null || total.intValue() > 0 ) {
			String hql = "select distinct r " + hqlPart + orderByClause;
			list = executeFind(hql, queryInfo);
			if ( total == null ) {
				total = new Integer(list.size());
			}
		} else {
			list = new ArrayList();
		}
		return new PartialCollection(list, total);

	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentImageDAO#findContentImageByUri(String)
	 */
	public ContentImage findContentImageByUri(String uri) {
		return (ContentImage) findUniqueResult("from ContentImage r where r.uri = ?", new Object[]{uri});
	}
}
